/*
 * $Id: LocationImportTemplateModelToLocationImportTemplateDTModelConverter.java 45 2013-05-03 20:32:47Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 45 $
 * $Date: 2013-05-03 20:32:47 +0000 (Fri, 03 May 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.service.support.converter;

import com.sci.logisticsmap.data.model.LocationImportTemplateModel;
import com.sci.logisticsmap.service.model.LocationImportTemplateDTModel;
import com.sci.logisticsmap.support.converter.BaseConverter;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * The class {@code LocationImportTemplateModelToLocationImportTemplateDTModelConverter} represents the converter
 * from {@link LocationImportTemplateModel} to {@link LocationImportTemplateDTModel}.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 45 $
 */
@Component
public class LocationImportTemplateModelToLocationImportTemplateDTModelConverter extends BaseConverter<LocationImportTemplateModel, LocationImportTemplateDTModel> {
    /*
     * (non-Javadoc)
     *
     * @see BaseConverter#convert(Object)
     */
    public LocationImportTemplateDTModel convert(LocationImportTemplateModel source) {
        LocationImportTemplateDTModel target = new LocationImportTemplateDTModel();
        target.setId(source.getId());
        target.setName(source.getName());
        target.setDescription(source.getDescription());
        target.setFileType(source.getFileType());
        target.setDelimiter(source.getDelimiter());
        target.setQualifier(source.getQualifier());
        target.setPrimary(source.isPrimary());
        target.setDefaultFlag(source.isDefaultFlag());
        if (source.getFields() != null) {
            target.setFields(convertFields(source.getFields(), target));
        }
        return target;
    }

    /**
     * Converts the Location import template field.
     *
     * @param source The source Location import template field.
     * @param targetParent The target Location import template.
     *
     * @return The converted Location import template field.
     */
    private LocationImportTemplateDTModel.Field convertLocationImportTemplateField(LocationImportTemplateModel.Field source, LocationImportTemplateDTModel targetParent) {
        LocationImportTemplateDTModel.Field target = targetParent.new Field();
        target.setType(source.getType());
        return target;
    }

    /**
     * Converts the Location import template fields.
     *
     * @param sources The source Location import template fields.
     * @param targetParent The target Location import template.
     *
     * @return The converted Location import template fields.
     */
    private List<LocationImportTemplateDTModel.Field> convertFields(List<LocationImportTemplateModel.Field> sources, LocationImportTemplateDTModel targetParent) {
        List<LocationImportTemplateDTModel.Field> targets = new ArrayList<LocationImportTemplateDTModel.Field>();
        for (LocationImportTemplateModel.Field source : sources) {
            targets.add(convertLocationImportTemplateField(source, targetParent));
        }
        return targets;
    }
}
